package cn.wx.org.controller;


import cn.wx.basic.exception.BusinessException;
import cn.wx.basic.util.JsonResult;
import cn.wx.basic.util.PageList;
import cn.wx.org.domain.Shop;
import cn.wx.org.domain.ShopLog;
import cn.wx.org.dto.ShopDto;
import cn.wx.org.query.ShopQuery;
import cn.wx.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/shop")
@Api(value = "店铺的API",description="部门相关的CRUD功能")
public class ShopController {

    @Autowired
    private IShopService iShopService;

    //使用get方式请求  查询所有
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Shop> findAll(){
        return iShopService.loadAll();
    }

    @GetMapping("/{id}")   //获取get请求里面的参数   查询一个
    @ApiOperation(value = "通过ID查询" )
    public Shop findById(@PathVariable("id") Long id ){
        return iShopService.loadById(id);
    }

    //删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            iShopService.remove(id);
           return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    //添加和更新     //使用put方式请求
    @PutMapping
    @ApiOperation(value = "增加修改" )
    public JsonResult save(@RequestBody Shop shop){
        try {
            if(shop.getId()==null){
                iShopService.save(shop);
            }else{
                iShopService.update(shop);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    //分页
    @PostMapping      //使用post方式请求
    @ApiOperation(value = "分页查询" )
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){

        try {
            return  iShopService.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    //批量删除
    @PatchMapping
    public JsonResult  patchDel(@RequestBody List<Long> ids){
        try {
            iShopService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }

    /*
    *   注册
    * */
    @PostMapping("/settlement")
    public JsonResult  settlement(@RequestBody Shop shop){
        try {
            iShopService.settlement(shop);
            return JsonResult.me();
        }catch (BusinessException e){   //业务问题
            e.printStackTrace();
            //代码问题
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg("系统问题，请稍后重试");
        }
    }

    /*
    * 审核失败
    * */
    @PostMapping("/audit/reject")
    public JsonResult  reject(@RequestBody ShopLog log){
        try {
            iShopService.reject(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("驳回失败");
        }

    }


    /*
     * 审核成功
     * */
    @PostMapping("/audit/pass")
    public JsonResult  pass(@RequestBody ShopLog log){
        try {
            iShopService.pass(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("审核失败");
        }

    }

    @GetMapping("/echarts")
    public List<ShopDto> echartsData(){
        List<ShopDto> shopDtos = iShopService.getCountByState();
        return shopDtos;
    }
}
